package com.atguigu.gmall.product.rest;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.entity.SpuImage;
import com.atguigu.gmall.model.product.entity.SpuInfo;
import com.atguigu.gmall.model.product.entity.SpuSaleAttr;
import com.atguigu.gmall.model.product.vo.SpuInfoVO;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
public class SpuController {

    @Autowired
    SpuInfoService spuInfoService;

    @Autowired
    SpuImageService spuImageService;

    @Autowired
    SpuSaleAttrService spuSaleAttrService;

    /**
     * /{page}/{limit}?category3Id=61
     */
    @GetMapping("/{page}/{limit}")
    public Result getSpuInfoPage(@RequestParam Long category3Id
                                ,@PathVariable Long page
                                ,@PathVariable Long limit
                                ) {
        Page<SpuInfo> pageList = spuInfoService.lambdaQuery().eq(SpuInfo::getCategory3Id, category3Id)
                .page(new Page<SpuInfo>(page, limit));
        return Result.ok(pageList);
    }


    /**
     * /admin/product/saveSpuInfo
     */
    @PostMapping ("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfoVO spuInfoVO) {
        spuInfoService.saveSpuInfo(spuInfoVO);
        return Result.ok();
    }

    /**
     * /admin/product/spuSaleAttrList/29
     */
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttrList(@PathVariable Long spuId) {
        List<SpuSaleAttr> list = spuSaleAttrService.getSpuSaleAttrList(spuId);
        return Result.ok(list);
    }

    /**
     * admin/product/spuImageList/29
     */
    @GetMapping("/spuImageList/{spuId}")
    public Result getSpuSpuImageList(@PathVariable Long spuId) {
        List<SpuImage> list = spuImageService.lambdaQuery()
                .eq(SpuImage::getSpuId, spuId).list();
        return Result.ok(list);
    }



}
